package com.codexsoft.test;

import com.codexsoft.test.entities.User;
import com.codexsoft.test.jpamodel.EMF;
import org.apache.wicket.extensions.wizard.Wizard;
import org.apache.wicket.extensions.wizard.WizardModel;
import javax.persistence.EntityManager;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: 23.08.11
 * Time: 16:24
 * To change this template use File | Settings | File Templates.
 */
public class RegistrationWizard extends Wizard {
    private static User user;

    public static User getUser() {
        return user;
    }

    public static void setUser(User user) {

        RegistrationWizard.user = user;
    }

    public RegistrationWizard(String id) {
        super(id);
        user = new User();
        WizardModel model = new WizardModel();
        model.add(new RegPanel1());
        model.add(new RegPanel2());
        model.add(new RegPanel3());
        init(model);
    }

    @Override
    public void onCancel() {
        setResponsePage(UInfoPage.class);
    }

    @Override
    public void onFinish() {
        EntityManager em = EMF.get().createEntityManager();
        String query = "SELECT FROM " + User.class.getName();
        List<User> userList = em.createQuery(query).getResultList();
        userList.add(user);
        try {
            em.persist(user);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
        }
        setResponsePage(UInfoPage.class);
    }

}
